#include "Parser.h"
#include <string>
#include <iostream>
#include <algorithm> // 包含 remove_if 函数
#include <cctype>    // 包含 isspace 函数
#include <limits>
using namespace std;

void Parser::split_string(string str, string op)
{
    std::string::size_type start = 0;
    std::string::size_type end = 0;

    // 去除字符串中的多余空格
    bool previous_space = false;
    for (char &c : str)
    {
        if (std::isspace(c))
        {
            if (!previous_space)
            {
                c = ' '; // 将连续的多个空格转换为一个空格
                previous_space = true;
            }
        }
        else
        {
            previous_space = false;
        }
    }

    // 移除字符串末尾的空格
    str.erase(std::find_if(str.rbegin(), str.rend(), [](unsigned char ch)
                           { return !std::isspace(ch); })
                  .base(),
              str.end());

    while ((end = str.find(op, start)) != std::string::npos)
    {
        processed_data.push_back(str.substr(start, end - start));
        start = end + op.length();
    }

    // 处理最后一个子字符串
    std::string lastSubstring = str.substr(start);
    if (!lastSubstring.empty())
        processed_data.push_back(lastSubstring);
}
bool isOnlySpaces(const std::string &str)
{
    for (char ch : str)
    {
        if (ch != ' ')
        {
            return false; // 如果遇到非空格字符，返回false
        }
    }
    return true; // 如果字符串中全部字符都是空格，返回true
}
bool intjudge(string suspect)
{
    for (char &c : suspect)
    {
        if (!isdigit(c))
            return false;
    }
    return true;
}

void Parser::add_argument(string str1, int num, string str2)
{
    if (str1 == "--first")
    {
        data["first"] = num;
    }
    if (str1 == "--second")
    {
        data["second"] = num;
    }
    if (str1 == "--third")
    {
        data["third"] = num;
    }
};

void Parser::init(char *args)
{
    string str_args = args;
    if (str_args.empty())
    {
        return;
    }
    split_string(str_args, " ");

    //--help情况
    for (auto str : processed_data)
    {
        if (str == "--help")
        {
            cout << "usage: ./main [--help] [--first FIRST] [--second SECOND] [--third THIRD]" << endl;
            cout << endl;
            cout << "optional arguments:" << endl;
            cout << "--help   show this help message and exit" << endl;
            cout << "--first FIRST   First number" << endl;
            cout << "--second SECOND   Next number" << endl;
            cout << "--third THIRD   Third number" << endl;
            exit(0);
        }
    }

    bool expect_argument = false;
    string current_option;

    for (int i = 0; i < processed_data.size(); i++)
    {

        if ((processed_data[i] == "--first" || processed_data[i] == "--second" || processed_data[i] == "--third") && expect_argument == false)
        {
            current_option = processed_data[i];
            expect_argument = true;
        }
        else if (expect_argument)
        {

            if (processed_data[i].empty() || isOnlySpaces(processed_data[i]))
            {
                cout << "usage: ./main [--help] [--first FIRST] [--second SECOND] [--third THIRD]" << endl;
                cout << "./main: error: argument " << current_option << ": expected an argument" << endl;
                exit(0);
            }

            if (intjudge(processed_data[i]))
            {
                add_argument(current_option, stoi(processed_data[i]), "");
            }
            else
            {
                cout << "usage: ./main [--help] [--first FIRST] [--second SECOND] [--third THIRD]" << endl;
                cout << "./main: error: argument " << current_option << ": invalid int value: '";
                cout << processed_data[i] << "'" << endl;
                exit(0);
            }

            expect_argument = false;
        }
        else
        {
            cout << "usage: ./main [--help] [--first FIRST] [--second SECOND] [--third THIRD]" << endl;
            cout << "./main: error: unrecognized arguments: " << processed_data[i] << endl;
            exit(0);
        }
    }

    if (expect_argument)
    {
        cout << "usage: ./main [--help] [--first FIRST] [--second SECOND] [--third THIRD]" << endl;
        cout << "./main: error: argument " << current_option << ": expected an argument" << endl;
        exit(0);
    }
}

int Parser::get_argument(char *ch)
{
    string str_ch = ch;
    return data[str_ch];
}
